@startuml
participant "Application\nCode" as A

skinparam Participant {
  RoundCorner 10

  BackgroundColor<<LLMRails >> 76b900
  FontColor<<LLMRails>> white
  BorderColor<<LLMRails>> green
}


participant LLMRails <<LLMRails>>
participant "Vector DB" as V


A->LLMRails: <<USER MESSAGE>>
activate LLMRails

note over LLMRails: Stage 1: Generate user intent.

  LLMRails->V: search canonical form examples
  activate V
  return Top K examples

  alt If the `use_embeddings_only` option is set

  LLMRails->LLMRails: Use closest match

  else If not, ask the LLM to generate the user intent
  LLMRails->LLM: generate_user_intent
  activate LLM
  return <<CANONICAL FORM>>

  end

note over LLMRails: Stage 2: Generate next step.

alt If a flow matches the user intent

LLMRails->LLMRails: <<NEXT STEP>>

else If not, generate next step using the LLM

LLMRails->V: search flow examples
activate V
return Top K examples

LLMRails->LLM: generate_next_steps
activate LLM
return <<NEXT STEP>>
end

note over LLMRails: Stage 3: Generate bot message.

  alt If a predefined message exists
    LLMRails->LLMRails: <<BOT_MESSAGE>>

  else If not, ask the LLM to generate the bot message
    LLMRails->V: search bot message examples
    activate V
    return Top K examples

    LLMRails->LLM: generate_bot_message
    activate LLM
    return <<BOT MESSAGE>>
  end


return #green <<BOT MESSAGE>>

participant LLM
hide footbox
hide stereotype
scale 2
@enduml
